﻿//********************************************************************************//
//创建日期：<创建日期，2016-06-13>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：报表打印
//********************************************************************************//
layui.use(['form', 'jquery', 'element', 'laydate'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , element = layui.element()
    , laydate = layui.laydate;

    /*initial load start*/
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var rqstr=year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
    initcontrol();
    load();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        

    }
    function load() {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/BaoBiao.ashx",
            data: {
                "op": "List"
            },
            dataType: "json",
            success: function (rs) {
                var fubbstrs="";
                var strtemp="";
                var childstr="";
                //加载报表类别
                for (var i = 0; i < rs.length; i++) {
                    if(rs[i]["I_FUID"]["Value"]=="0")
                    {
                        strtemp="<div class='layui-colla-item' vid='"+rs[i]["I_BAOBIAOID"]["Value"]+"'><h2 class='layui-colla-title'><b style='color:blue;font-size:bold'>"
                        + rs[i]["S_BAOBIAOMC"] + "</b></h2><div class='layui-colla-content'><table class='layui-table' style='width:100%;' ></table></div></div>";
                        fubbstrs=fubbstrs+strtemp;
                    }
                }
                $("#divList").append(fubbstrs);
                //加载具体报表
                $("#divList .layui-colla-item").each(function () {
                    childstr = "";
                    for (var i = 0; i < rs.length; i++) {
                        if (rs[i]["I_FUID"]["Value"] == $(this).attr("vid")) {
                            strtemp = "<tr class='datarow' style='cursor:pointer' vid='" + rs[i]["I_BAOBIAOID"]["Value"] + "'><td style='padding-left:20px;font-size:14px'><b>" + rs[i]["S_BAOBIAOMC"] + "</b></td></tr>";
                            childstr = childstr + strtemp;
                        }
                    }
                    $(this).find("table").append(childstr);
                });
                element.init();
                //绑定报表行点击事件
                $(".datarow").click(function () {
                    //清除所有背景
                    $(".datarow").each(function () {
                        $(this).css("background-color","");
                    });
                    //设置当前选中背景色
                    $(this).css("background-color", "greenyellow");
                    //显示报表标题
                    $("#iTitle").text($(this).find("td").text());
                    $("#iTitle").attr("vid",$(this).attr("vid"));
                    loadCSs($(this).attr("vid"));
                    $("#divTitle").show();
                    $("#divBtns").show();
                    //第一个参数获取焦点
                    $("#divCSList").find("[name = 'cs']").eq(0).focus();
                });
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }
    //加载报表参数
    function loadCSs(bbid)
    {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/BaoBiao.ashx",
            data: {
                "op": "CSList",
                "bbid":bbid
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                var inputstr = "";
                
                $("#divCSList .layui-form-item").remove();
                for (var i = 0; i < rs.length; i++) {
                    str = "<div class='layui-form-item' vid='" + rs[i]["S_CANSHU"] + "'><div class='layui-inline'><label class='layui-form-label' style='width:120px;font-size:16px'>"
                        + rs[i]["S_CANSHUMC"] +
                        ":</label><div class='layui-input-inline' style='width: 130px;'>inputstr</div></div></div>";
                    if (rs[i]["I_CANSHULX"]["Value"] == "0") {
                        if (rs[i]["S_CANSHU"] == "V_DAYINRQ") {
                            inputstr = "<input name='cs' vid='" + rs[i]["S_CANSHU"] + "' autocomplete='off' class='layui-input' value='" + rqstr + "' onclick='layui.laydate({ elem: this, festival: true})'>";
                        }
                        else {
                            inputstr = "<input name='cs' vid='" + rs[i]["S_CANSHU"] + "' autocomplete='off' class='layui-input' value='" + rs[i]["S_CANSHUZ"] + "'>";
                        }
                    }
                    else if (rs[i]["I_CANSHULX"]["Value"] == "1") {
                        inputstr = "<select name='cs' vid='" + rs[i]["S_CANSHU"] + "'>";
                        //加载筛选值列表
                        var cszList = getCSZList(bbid, rs[i]["S_SHUJUY"]);
                        for (var j = 0; j < cszList.length; j++) {
                            if (rs[i]["S_MORENZ"] == cszList[j]["Value"])
                            {
                                inputstr = inputstr + "<option value='" + cszList[j]["Value"] + "' selected='selected'>" + cszList[j]["Text"] + "</option>";
                            }
                            else
                            {
                                inputstr = inputstr + "<option value='" + cszList[j]["Value"] + "'>" + cszList[j]["Text"] + "</option>";
                            }
                        }
                        inputstr = inputstr + "</select>";
                    }
                    str = str.replace("inputstr",inputstr);
                    strs = strs + str;
                }                
                $("#divCSList").append(strs);
                form.render("select");
                bindCSEvent();
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }
    //获取筛选类型参数值列表(报表id,参数值数据源)
    function getCSZList(bbid,cssjy)
    {
        var rss;
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/BaoBiao.ashx",
            data: {
                "op": "CSZList",
                "bbid": bbid,
                "cssjy":cssjy
            },
            dataType: "json",
            success: function (rs) {
                rss=rs;
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        return rss;
    }
    function bindCSEvent()
    {       
        $("#divCSList").find("[name = 'cs']").keydown(function () {
            if (event.keyCode == 13) {
                var nextControl=$("#divCSList .layui-form-item[vid='" + $(this).attr("vid") + "']").next().find("[name = 'cs']");
                if ($(nextControl).length == 1) {
                    while($(nextControl).find("option").length>0)
                    {
                        nextControl = $("#divCSList .layui-form-item[vid='" + $(nextControl).attr("vid") + "']").next().find("[name = 'cs']");
                    }
                    $(nextControl).focus();
                    
                }
                else {
                    $("#oViewStop").focus();
                }
            }
            
        });
    }
    $("#oViewStop").keydown(function () {
        if (event.keyCode == 13) {
            $("#oView").focus();
        }
    });
    $("#oViewStop").focus(function () {
        $("#oView").css("border", "1px solid green");
        $("#oView").css("background-color", "greenyellow");
    });
    $("#oViewStop").blur(function () {
        $("#oView").css("border", "");
        $("#oView").css("background-color", "");
    });
    //预览按钮事件
    $("#oView").click(function () {
        var csArr = new Array();
        //获取参数
        $("#divCSList").find("[name = 'cs']").each(function () {
            csArr.push($(this).attr("vid")+":"+$(this).val());
        });
        var paramsStr = csArr.join("|");
        //var url = "BaoBiaoView.aspx?baobiaoid=" + $("#iTitle").attr("vid") + "&x=" + 0 + "&y=" + 0 + "&baobiaocs=" + paramsStr;
        //$(this).prop("disabled", "disabled");
        //TODO: 郭登杰要修改用户信息，用于偏移量判断
        wap.common.printjx('ZhangDanDY', [
            { key: 'QUERYPARAM', value: paramsStr },
            { key: 'BAOBIAOID', value: $("#iTitle").attr("vid") },
            { key: 'USERINFO', value: "csms" }
        ]);
        //layer.open({
        //    type: 2,
        //    title: "",
        //    shadeClose: false,
        //    shade: 0.8,
        //    area: ['99%', '630px'],
        //    content: url,
        //    cancel: function(index) {
        //        $("#oView").removeProp("disabled");
        //    }
        //});
    });

    /*end initial*/

    /*event*/
    
    /*event end*/

    function verify() {
        return true;
    }

    /*logic end*/
});